/**
 * 
 * @authors Your Name (you@example.org)
 * @date    2018-01-02 13:28:52
 * @version $Id$
 */

// var url = window.location.search;
// 	if (url.indexOf("?") != -1) {
// 		var str = url.substr(1)
// 		strs = str.split("&");
// 			var number = parseInt(strs[0].split("=")[1])  ;

// 	}
// 	
// 	
var number = GetQueryString("commodityNumber")
window.addEventListener("load",function(){
	browHistory(11)
})



/*增加浏览历史*/
function browHistory(price){

	mui.ajax({
		"url":URL_all+'client/record/insertrecord',
		"data":"{'commodityNumber':'"+number+"','previewPrice':'"+price+"'}",
		"dataType":"json",
		"type":"post",
		"timeout":10000000000,
		"headers":{'userToken':localStorage.userToken,'Content-Type':'application/json'},
		"success":function(data){
			
			console.log(localStorage.userToken)
			console.log(JSON.stringify(data))
		},
		"error":function(xhr,type,errorThrown){
			console.log(type);
		}
	})
}


/*获取地址栏中的commodityNumber 查询商品详细信息*/		

	mui.ajax(URL_all+'api/bizcommodity/selectcommodityinfo?page=1&pagesize=10',{
	data:{
		sorting:'commodity_name',
		commodityNumber:number,
		tosort:'desc'
	},
	dataType:'json',//服务器返回json格式数据
	type:'post',//HTTP请求类型
	timeout:10000,//超时时间设置为10秒；
	headers:{'Content-Type':'application/json'},	              
	success:function(data){
		//服务器返回响应，根据响应结果，分析是否登录成功；
		
		console.log(data)
		// $(".list-wrapper").empty();
		mui(".PD_name")[0].innerText=data.pageInfo.list[0].commodityName;
		mui(".commodityOrigin")[0].innerText=data.pageInfo.list[0].commodityOrigin;
		mui(".commodityApply")[0].innerText=data.pageInfo.list[0].commodityApply;
		mui(".commoditySet")[0].innerText=data.pageInfo.list[0].commoditySet;
	},
	error:function(xhr,type,errorThrown){
		//异常处理；
		console.log(type);
	}
});



$(".add_car").on("tap",function(){
	addCar()
})


function insertdetailed(commodityNumber,needNumber,add_price,add_time,callback){
	mui.ajax({
		"url":URL_all+"client/detailed/insertdetailed",
		"data":"{'commodityNumber':'"+commodityNumber+"','needNumber':'"+needNumber+"','add_price':'"+add_price+"','add_time':'"+new Date().getTime()+""+"','newNeedNumber':'"+needNumber+"'}",
		"dataType":"json",
		"type":"post",
		"timeout":10000000000,
		"headers":{'userToken':localStorage.userToken,'Content-Type':'application/json'},
		"success":function(data){
			
			console.log(localStorage.userToken)
			console.log(data)
			var result = [{'commodityNumber':commodityNumber,'count':needNumber}];
            result = JSON.stringify(result);
            localStorage.setItem('goodsList',result);
			callback&&callback(data);
		},
		"error":function(xhr,type,errorThrown){
			console.log(type);
		}
	})
}



function addCar(){
	var commodityNumber = GetQueryString("commodityNumber");
	var price = 1;
	var goodsNum = 1;
	insertdetailed(commodityNumber,goodsNum,price,(new Date().getTime()),
		function(data){
			console.log(data)
		var sta = data.htmlState.status
		if(sta!=200){
			mui.toast('添加失败')
		}
		else{
			mui.toast('加入购物车成功!')
		
	// 		mui.ajax({
	// 			"url":"http://hsmytest.vrdete.com/client/detailed/selectbyuserId?adcode=100000&page=1&pagesize=12",
	// 			"dataType":"json",
	// 			"type":"get",
	// 			"timeout":10000000000,
	// 			"headers":{'userToken':localStorage.userToken},
	// 			"success":function(data){
	// 				console.log(localStorage.userToken);
	// 				console.log(JSON.stringify(data));
				
		
	// 		},
	// 		"error":function(xhr,type,errorThrown){
	// 			console.log(type);
	// 		}
	// })
		}
	})
}

$(".gouwuche").on("tap",function(){
letGoBuy()
	
})


function letGoBuy(){
    var commodityNumber = GetQueryString("commodityNumber");
    // var price = mui(".price")[0].innerHTML.replace(/[^0-9]/ig,"");//价格参数，将非数字部分用空格代替
    // var goodsNum = mui(".num")[0].innerText;//数量参数
	// var count = domId.parents(".product").find(".product_Number span").text();
	var count = 1;
    var result = [{'commodityNumber':commodityNumber,'count':count}];
    result = JSON.stringify(result);
    localStorage.setItem('goodsList',result);
	window.location.href = "order_confirm.html";
	// openOpacity(mask);
}